import service from '..'
import { mockRoom } from '@/mock'

// 判断是否使用mock数据
const useMock = import.meta.env.VITE_MOCK_MODE === 'true' || import.meta.env.VITE_MOCK_MODE === true

/**
 * 群房间用户分页列表
 * @param {*} params
 * @returns
 */
export function roomGroupUserPageApi(params) {
  if (useMock) {
    return mockRoom.roomGroupUserPage(params)
  }
  return service({
    url: '/api/roomGroupUser/page',
    method: 'get',
    params
  })
}

/**
 * 群房间用户数量
 * @param {*} params
 * @returns
 */
export function roomGroupUserCountApi(params) {
  if (useMock) {
    return mockRoom.roomGroupUserCount(params)
  }
  return service({
    url: '/api/roomGroupUser/count',
    method: 'get',
    params: {
      room_id: params.room_id
    }
  })
}

/**
 * 创建群聊
 * @param {*} data
 * @returns
 */
export function createRoomApi(data) {
  if (useMock) {
    return mockRoom.createRoom(data)
  }
  return service({
    url: '/api/roomGroupUser/create',
    method: 'post',
    data: {
      name: data.name,
      description: data.description,
      avatar: data.avatar
    }
  })
}

/**
 * 获取用户加入的群聊列表
 * @returns
 */
export function listRoomsApi() {
  if (useMock) {
    return mockRoom.listRooms()
  }
  return service({
    url: '/api/roomGroupUser/list',
    method: 'get'
  })
}

/**
 * 获取群聊详情
 * @param {*} id 群聊ID
 * @returns
 */
export function getRoomDetailApi(id) {
  if (useMock) {
    return mockRoom.getRoomDetail(id)
  }
  return service({
    url: `/api/roomGroupUser/detail/${id}`,
    method: 'get'
  })
}

/**
 * 邀请用户加入群聊
 * @param {*} data
 * @returns
 */
export function inviteUsersApi(data) {
  if (useMock) {
    return mockRoom.inviteUsers(data)
  }
  return service({
    url: '/api/roomGroupUser/invite',
    method: 'post',
    data
  })
}

/**
 * 退出群聊
 * @param {*} id 群聊ID
 * @returns
 */
export function exitRoomApi(id) {
  if (useMock) {
    return mockRoom.exitRoom(id)
  }
  return service({
    url: `/api/roomGroupUser/exit/${id}`,
    method: 'post'
  })
}

/**
 * 解散群聊
 * @param {*} id 群聊ID
 * @returns
 */
export function disbandRoomApi(id) {
  if (useMock) {
    return mockRoom.disbandRoom(id)
  }
  return service({
    url: `/api/roomGroupUser/${id}`,
    method: 'delete'
  })
}

/**
 * 搜索群聊
 * @param {*} params 搜索参数
 * @returns
 */
export function searchGroupsApi(params) {
  if (useMock) {
    return mockRoom.searchGroups(params)
  }
  return service({
    url: '/api/roomGroupUser/search',
    method: 'get',
    params
  })
}

/**
 * 申请加入群聊
 * @param {number} roomId 群聊ID
 * @returns {Promise}
 */
export function joinGroupApi(roomId) {
  if (useMock) {
    return mockRoom.joinGroup(roomId)
  }
  return service({
    url: `/api/roomGroupUser/join/${roomId}`,
    method: 'post'
  })
}

/**
 * 获取群聊申请列表
 * @param {Object} params 查询参数
 * @param {number} params.room_id 群聊ID
 * @param {number} [params.status=0] 申请状态 0-待审核 1-已通过 2-已拒绝
 * @param {number} [params.page=1] 页码
 * @param {number} [params.page_size=20] 每页数量
 * @returns {Promise}
 */
export function getRoomAppliesApi(params) {
  return service({
    url: '/api/roomGroupUser/applies',
    method: 'get',
    params
  })
}

/**
 * 审核群聊申请
 * @param {Object} data 审核数据
 * @param {number} data.id 申请ID
 * @param {number} data.status 审核状态 1-通过 2-拒绝
 * @returns {Promise}
 */
export function reviewRoomApplyApi(data) {
  return service({
    url: '/api/roomGroupUser/apply/review',
    method: 'post',
    data
  })
}
